package com.bw.module_cashier.view

import android.app.DatePickerDialog
import android.widget.DatePicker
import android.widget.PopupMenu
import android.widget.Toast
import androidx.lifecycle.ViewModelProvider
import com.alibaba.android.arouter.facade.annotation.Autowired
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.bw.lib_base.path.RouterPath
import com.bw.lib_base.view.BaseActivity
import com.bw.lib_common.entity.CashierBillEntity
import com.bw.lib_common.entity.CashierPayerEntity
import com.bw.module_cashier.R
import com.bw.module_cashier.databinding.ActivityTemporaryChargesBinding
import com.bw.module_cashier.viewmodel.CashierViewModel
import dagger.hilt.android.AndroidEntryPoint
import java.text.SimpleDateFormat
import java.util.*

/**
 * 智慧园区-05-收银台V1.1-20250509
 */
@AndroidEntryPoint
@Route(path = RouterPath.PATH_TEMPORARY_CHARGES_ACTIVITY)
class TemporaryChargesActivity : BaseActivity<ActivityTemporaryChargesBinding>() {
    private val vm by lazy { ViewModelProvider(this)[CashierViewModel::class.java] }
    @Autowired(name = "cashierPayerEntity")
    lateinit var entity: CashierPayerEntity
    override fun initView() {
        ARouter.getInstance().inject(this)
        mDataBinding.projectName.text = entity.projectName
        mDataBinding.start.setOnClickListener {

            val calendar: Calendar = Calendar.getInstance()
            val year: Int = calendar.get(Calendar.YEAR)
            val month: Int = calendar.get(Calendar.MONTH)
            val day: Int = calendar.get(Calendar.DAY_OF_MONTH)
            var date = ""

            val datePickerDialog = DatePickerDialog(
                this,
                { view: DatePicker?, selectedYear: Int, selectedMonth: Int, selectedDay: Int ->
                    date = selectedYear.toString() + "-" + (selectedMonth + 1) + "-" + selectedDay
                    mDataBinding.startTime.setText(date)
                },
                year, month, day
            )

            datePickerDialog.datePicker.minDate = System.currentTimeMillis() - 1000

            datePickerDialog.show()
        }
        mDataBinding.end.setOnClickListener {
            val calendar: Calendar = Calendar.getInstance()
            val year: Int = calendar.get(Calendar.YEAR)
            val month: Int = calendar.get(Calendar.MONTH)
            val day: Int = calendar.get(Calendar.DAY_OF_MONTH)
            var date = ""

            val datePickerDialog = DatePickerDialog(
                this,
                { view: DatePicker?, selectedYear: Int, selectedMonth: Int, selectedDay: Int ->
                    date = selectedYear.toString() + "-" + (selectedMonth + 1) + "-" + selectedDay
                    mDataBinding.endTime.setText(date)
                },
                year, month, day
            )

            datePickerDialog.datePicker.minDate = System.currentTimeMillis() - 1000

            datePickerDialog.show()
        }

        mDataBinding.project.setOnClickListener {
            val popupMenu = PopupMenu(this, it)
            popupMenu.menuInflater.inflate(R.menu.project_menu, popupMenu.menu)

            popupMenu.setOnMenuItemClickListener { item ->
                when (item.itemId) {
                    R.id.item1 -> {
                        mDataBinding.chargeType.setText("物业费")
                        true
                    }
                    R.id.item2 -> {
                        mDataBinding.chargeType.setText("停车费")
                        true
                    }
                    else -> false
                }
            }

            popupMenu.show()
        }

        mDataBinding.sure.setOnClickListener {
            vm.addCashier(
                CashierBillEntity(
                    id = 0,
                    actualAmount = mDataBinding.totalMoney.text.toString().toInt(),
                    balanceDeduction = 0,
                    chargeAmount = mDataBinding.totalMoney.text.toString().toInt(),
                    chargeType = mDataBinding.chargeType.text.toString(),
                    contactNumber = "17382856680",
                    discountAmount = 0,
                    effectiveDate = mDataBinding.startTime.text.toString(),
                    expirationDate = mDataBinding.endTime.text.toString(),
                    operator = "琉璃月",
                    paymentMethod = "现金",
                    paymentStatus = "未缴费",
                    paymentTime = SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(Date()),
                    penalty = 0,
                    receivableAmount = mDataBinding.totalMoney.text.toString().toInt(),
                    payerId = entity.payerId,
                    payerName = entity.payerName,
                    checked = false
                )
            )
        }

        vm.addCashierBillSuccess.observe(this){
            if (it.paymentStatus == "未缴费"){
                Toast.makeText(this,"添加成功",Toast.LENGTH_SHORT).show()
                finish()
            }else{
                Toast.makeText(this,"添加失败",Toast.LENGTH_SHORT).show()
            }
        }
        mDataBinding.imageView.setOnClickListener {
            finish()
        }
    }

    override fun getLayoutId(): Int = R.layout.activity_temporary_charges
}